﻿using AutoMapper;
using Customer.API.Applcation.Command.MeetCommand;
using Customer.Domain;
using Customer.Infrstructre.Meet;
using MediatR;

namespace Customer.API.Applcation.CommandHandlers.MeetCommandHandler
{
    public class UpdMeetCommandHandler:IRequestHandler<UpdMeetCommand,int>
    {
        private readonly IAccountsRepository accountRep;
        private readonly IMapper mapper;

        public UpdMeetCommandHandler(IAccountsRepository accountRep, IMapper mapper)
        {
            this.accountRep = accountRep;
            this.mapper = mapper;
        }

        public async Task<int> Handle(UpdMeetCommand request, CancellationToken cancellationToken)
        {
            var res = (await accountRep.GetAllAsync()).Where(x => x.AccountsName == request.updDTO.AccountsName);
            if(res.Count() == 0)
            {
                var s = (await accountRep.GetAllAsync()).FirstOrDefault(x=>x.AccountsId==request.updDTO.AccountsId);
                return await accountRep.Update(mapper.Map(request.updDTO,s));
            }
            else
            {
                return -1;//名称存在，不能修改
            }
        }
    }
}
